package com.example.studentapp.login;

import androidx.appcompat.app.AppCompatActivity;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.studentapp.R;
import com.example.studentapp.db.SQLiteDB;

public class GetPwdActivity extends AppCompatActivity {
    ImageView get_pwd_returnIV;
    EditText get_pwd_phoneET,get_pwd_emailET,get_pwd_pwdET,get_pwd_mk_pwdET;
    Button get_pwdBtn;
    TextView get_pwd_saveTV;
    SQLiteDB sqLiteDB;
    SQLiteDatabase sqLiteDatabase;
    String phone;

    // 找回密码
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_get_pwd);
        init();
        get_pwd_returnIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        // 验证用户账号显示新密码设置
        get_pwdBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                phone = get_pwd_phoneET.getText().toString();
                String email = get_pwd_emailET.getText().toString();
                if(phone.length()>0 && email.length() >0){
                    sqLiteDB = new SQLiteDB(getApplicationContext());
                    sqLiteDatabase = sqLiteDB.getWritableDatabase();
                    // 查询数据
                    String sql = "select * from user_info where phone= '" + phone +"'and email='" + email + "'";
                    Cursor cursor = sqLiteDatabase.rawQuery(sql,null);
                    Log.i("TAG", String.valueOf(cursor.getCount()));

                    if(cursor.getCount() >0){
                        get_pwd_pwdET.setVisibility(View.VISIBLE);
                        get_pwd_mk_pwdET.setVisibility(View.VISIBLE);
                        get_pwd_saveTV.setVisibility(View.VISIBLE);
                    }else{
                        Toast.makeText(getApplicationContext(), getString(R.string.login_toast_error), Toast.LENGTH_SHORT).show();
                    }
                }else{
                    Toast.makeText(getApplicationContext(), getString(R.string.login_toast_null), Toast.LENGTH_SHORT).show();
                }
            }
        });

        // 保存新密码入库，然后返回登录界面
        get_pwd_saveTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String newPwd = get_pwd_pwdET.getText().toString();
                String new2pwd = get_pwd_mk_pwdET.getText().toString();
                if(newPwd.length() > 0 && new2pwd.length() > 0){
                    if(newPwd.equals(new2pwd)){
                        // 更改密码
                        String updateSql = "update user_info set pwd='" + newPwd + "' where phone='" + phone + "'";
                        try {
                            sqLiteDatabase.execSQL(updateSql);
                            Toast.makeText(getApplicationContext(), getString(R.string.db_toast_pwd_success), Toast.LENGTH_SHORT).show();
                            finish();

                        }catch (Exception e){
                            Toast.makeText(getApplicationContext(), getString(R.string.db_toast_pwd_failed), Toast.LENGTH_SHORT).show();
                        }
                    }else{
                        Toast.makeText(getApplicationContext(), getString(R.string.login_toast_same), Toast.LENGTH_SHORT).show();
                    }
                }else{
                    Toast.makeText(getApplicationContext(), getString(R.string.login_toast_null), Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void init() {
        get_pwd_returnIV = findViewById(R.id.get_pwd_returnIV);
        get_pwd_phoneET = findViewById(R.id.get_pwd_phoneET);
        get_pwd_emailET = findViewById(R.id.get_pwd_emailET);
        get_pwd_pwdET = findViewById(R.id.get_pwd_pwdET);
        get_pwd_mk_pwdET = findViewById(R.id.get_pwd_mk_pwdET);
        get_pwdBtn = findViewById(R.id.get_pwdBtn);
        get_pwd_saveTV = findViewById(R.id.get_pwd_saveTV);
    }
}